package com.rjj.wlqq.wxpay.wxpay.entity;

import com.rjj.wlqq.wx.tool.StrKit;
import com.rjj.wlqq.wxpay.wxpay.tool.NumberKit;
import com.rjj.wlqq.wxpay.wxsdk.WXPayConstants;

/**
 * @author r
 * @date 2021/1/19 20:48
 * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
 * <xml>
 * <appid>wx2421b1c4370ec43b</appid>
 * <attach>支付测试</attach>
 * <body>JSAPI支付测试</body>
 * <mch_id>10000100</mch_id>
 * <detail><![CDATA[{ "goods_detail":[ { "goods_id":"iphone6s_16G", "wxpay_goods_id":"1001", "goods_name":"iPhone6s 16G", "quantity":1, "price":528800, "goods_category":"123456", "body":"苹果手机" }, { "goods_id":"iphone6s_32G", "wxpay_goods_id":"1002", "goods_name":"iPhone6s 32G", "quantity":1, "price":608800, "goods_category":"123789", "body":"苹果手机" } ] }]]></detail>
 * <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
 * <notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>
 * <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
 * <out_trade_no>1415659990</out_trade_no>
 * <spbill_create_ip>14.23.150.211</spbill_create_ip>
 * <total_fee>1</total_fee>
 * <trade_type>JSAPI</trade_type>
 * <sign>0CB01533B8C1EF103065174F50BCA001</sign>
 * </xml>
 */
public class WxOrder
{
    private String out_trade_no;
    private String trade_type;
    private String openid;
    private String total_fee;
    private String profitSharing;
    private String spbill_create_ip;
    private String attach;
    private String notify_url;
    private String body;
    private String product_id;


    private WxOrder()
    {
    }

    public WxOrder(String out_trade_no, String trade_type, String openid, String total_fee, String profitSharing, String spbill_create_ip, String attach, String notify_url, String body)
    {
        this.out_trade_no = out_trade_no;
        this.trade_type = trade_type;
        this.openid = openid;
        this.total_fee = total_fee;
        this.profitSharing = profitSharing;
        this.spbill_create_ip = spbill_create_ip;
        this.attach = attach;
        this.notify_url = notify_url;
        this.body = body;
    }

    public String getProduct_id()
    {
        return product_id;
    }

    public WxOrder setProduct_id(String product_id)
    {
        this.product_id = product_id;
        return this;
    }

    public String getOut_trade_no()
    {
        return out_trade_no;
    }

    public WxOrder setOut_trade_no(String out_trade_no)
    {
        this.out_trade_no = out_trade_no;
        return this;
    }

    public String getTrade_type()
    {
        return trade_type;
    }

    public WxOrder setTrade_type(String trade_type)
    {
        this.trade_type = trade_type;
        return this;
    }

    public String getOpenid()
    {
        return openid;
    }

    public WxOrder setOpenid(String openid)
    {
        this.openid = openid;
        return this;
    }

    public String getTotal_fee()
    {
        return NumberKit.toPenny(Double.parseDouble(total_fee));
    }

    public WxOrder setTotal_fee(String total_fee)
    {
        this.total_fee = total_fee;
        return this;
    }

    public String getProfitSharing()
    {
        return profitSharing;
    }

    public WxOrder setProfitSharing(String profitSharing)
    {
        this.profitSharing = profitSharing;
        return this;
    }

    public String getSpbill_create_ip()
    {
        return spbill_create_ip;
    }

    public WxOrder setSpbill_create_ip(String spbill_create_ip)
    {
        this.spbill_create_ip = spbill_create_ip;
        return this;
    }

    public String getAttach()
    {
        return attach;
    }

    public WxOrder setAttach(String attach)
    {
        this.attach = attach;
        return this;
    }

    public String getNotify_url()
    {
        return notify_url;
    }

    public WxOrder setNotify_url(String notify_url)
    {
        this.notify_url = notify_url;
        return this;
    }

    public String getBody()
    {
        return body;
    }

    public WxOrder setBody(String body)
    {
        this.body = body;
        return this;
    }

    public static WxOrder Builder()
    {
        return new WxOrder();
    }

    public WxOrder build()
    {
        if (StrKit.isBlank(this.out_trade_no))
        {
            throw new IllegalStateException("out_trade_no is null");
        } else if (StrKit.isBlank(this.total_fee))
        {
            throw new IllegalStateException("total_fee is null");
        } else if (StrKit.isBlank(this.trade_type))
        {
            throw new IllegalStateException("trade_type is null");
        } else if (StrKit.isBlank(this.notify_url))
        {
            throw new IllegalStateException("notify_url is null");
        } else if (StrKit.isBlank(this.body))
        {
            throw new IllegalStateException("body is null");
        } else if (StrKit.isBlank(this.spbill_create_ip))
        {
            throw new IllegalStateException("spbill_create_ip is null");
        } else if (trade_type.equals(WXPayConstants.TradeType.JSAPI.toString()) && StrKit.isBlank(this.openid))
        {
            throw new IllegalStateException("JSAPI ===> openid do not  null");
        } else if (trade_type.equals(WXPayConstants.TradeType.NATIVE.toString()) && StrKit.isBlank(this.product_id))
        {
            throw new IllegalStateException("NATIVE ===> product_id do not  null");
        } else if (!trade_type.equals(WXPayConstants.TradeType.NATIVE.toString()) && !trade_type.equals(WXPayConstants.TradeType.JSAPI.toString()))
        {
            throw new IllegalStateException("support 【NATIVE，JSAPI】, do not support other type");
        } else
        {
            return this;
        }
    }
}
